package com.platform.hjt.flow.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.platform.hjt.base.controller.BaseController;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.flow.model.FlowFrom;
import com.platform.hjt.flow.service.FlowFromService;
import com.platform.hjt.from.service.FromDefaultDataService;

@Controller
@RequestMapping(value="/flowFrom")
public class FlowFromController extends BaseController{

	@Resource
	private FromDefaultDataService fromDefaultDataService;
	
	@Resource
	private FlowFromService flowFromService;
	
	@RequestMapping(value="/flowFrom_list.html")
	public String flowFromListPage(){
		return "page/admin/flowFrom/flowFrom_list";
	}
	
	@RequestMapping(value="/flowFrom_list.do")
	@ResponseBody
	public Page<FlowFrom> flowFromListDo(Page<FlowFrom>page){
		List<PropertyFilter>  propertyFilterList = PropertyFilter.buildFromHttpRequest(getRequest());
		propertyFilterList.add(new PropertyFilter("EQB_isDel", "false"));
		return flowFromService.getFlowFromList(page, propertyFilterList);
	}
	
	@RequestMapping(value="/flowFrom_edit.html")
	public String flowFromEditPage(HttpServletRequest request, Model model, String id){
		if(StringUtils.isNotBlank(id)){
			model.addAttribute("flowFrom", flowFromService.getFlowFrom("flowFromId", id));
		}
		
		return "page/admin/flowFrom/flowFrom_edit";
	}
	
	@RequestMapping(value="/flowFrom_detail_{id}.html")
	public String flowFromDetailPage(Model model, @PathVariable String id){
		model.addAttribute("flowFrom", flowFromService.getFlowFrom("flowFromId", id));
		return "page/admin/flowFrom/flowFrom_detail";
	}
	
	@RequestMapping(value="/flowFrom_edit.do")
	@ResponseBody
	public ReturnBody flowFromEditDo(@Valid @ModelAttribute("flowFrom") FlowFrom flowFrom, BindingResult result){
		if(result.hasErrors()){
			return new ReturnBody(false, result.getAllErrors().get(0).getDefaultMessage());
		}
		return flowFromService.saveFlowFrom(flowFrom);
	}
	
	@RequestMapping(value="/flowFrom_del_{id}.do")
	@ResponseBody
	public ReturnBody flowFromDelDo(@PathVariable String id){
		return flowFromService.delFlowFrom(new FlowFrom(id));
	}
	
	@RequestMapping(value="/fromView.html")
	public String fromView(Model model, String fromId, String processId, String orderId, String taskId, String taskName, boolean readonly){
		
		model.addAttribute("processId", processId);
		model.addAttribute("orderId", orderId);
		model.addAttribute("taskId", taskId);
		model.addAttribute("taskName", taskName);
		
		/*System.out.println("--------------start--------------");
		System.out.println("orderId = " + orderId);
		System.out.println("taskId = " + taskId);
		System.out.println("readonly = " + readonly);
		System.out.println("--------------end--------------");*/
		
		FlowFrom flowFrom = flowFromService.getFlowFrom("flowFromId", fromId);
		//if(StringUtils.isNotBlank(orderId) && StringUtils.isNotBlank(taskId)){
		if(readonly){
			if(flowFrom.getIsDefault()){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("orderId", orderId);
				map.put("taskName", taskName);
				model.addAttribute("fdata", fromDefaultDataService.getFromDefaultData(map));
				return "page/admin/flowFrom/from/default/view";
			} else{
				return flowFrom.getFlowFromPath() + flowFrom.getFlowFromView();
			}
		} else{
			if(flowFrom.getIsDefault()){
				return "page/admin/flowFrom/from/default/from";
			} else{
				return flowFrom.getFlowFromPath() + flowFrom.getFlowFromFile();
			}
				
		}
	}
	
}
